package com.flynn.rock.crm.controller;

import com.flynn.rock.annotation.OperateLog;
import com.flynn.rock.crm.model.employee.request.PageEmployeeRequest;
import com.flynn.rock.crm.model.employee.request.ReceiveOrdersRequest;
import com.flynn.rock.crm.model.employee.request.SaveEmployeeRequest;
import com.flynn.rock.crm.model.employee.vo.EmployeeIdVo;
import com.flynn.rock.crm.model.employee.vo.EmployeeVo;
import com.flynn.rock.crm.model.employee.vo.ReceiveOrdersConfigVo;
import com.flynn.rock.crm.service.employee.EmployeeService;
import com.flynn.rock.enums.BusinessType;
import com.flynn.rock.enums.OperateType;
import com.flynn.rock.model.PageResult;
import com.flynn.rock.model.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/crm/employee")
@Tag(name = "员工管理")
public class EmployeeController {

    private EmployeeService employeeService;

    @PostMapping
    @Operation(summary = "新增员工信息")
    @OperateLog(operateTitle = "新增员工信息", operateType = OperateType.CREATE, businessType = BusinessType.CRM)
    public Response<EmployeeIdVo> insertEmployee(@RequestBody @Validated SaveEmployeeRequest request) {
        if (!StringUtils.hasText(request.getPassword())) {
            request.setPassword("Admin123");
        }
        return Response.success(employeeService.saveEmployee(request));
    }

    @PutMapping
    @Operation(summary = "修改员工信息")
    @OperateLog(operateTitle = "修改员工信息", operateType = OperateType.UPDATE, businessType = BusinessType.CRM)
    public Response<EmployeeIdVo> updateEmployee(@RequestBody @Validated SaveEmployeeRequest request) {
        return Response.success(employeeService.saveEmployee(request));
    }

    @GetMapping("/{employeeId}")
    @Operation(summary = "查询员工信息")
    public Response<EmployeeVo> getEmployee(@PathVariable("employeeId") Long employeeId) {
        return Response.success(employeeService.getEmployee(employeeId));
    }

    @DeleteMapping("/{employeeId}")
    @Operation(summary = "删除员工信息")
    @OperateLog(operateTitle = "删除员工信息", operateType = OperateType.DELETE, businessType = BusinessType.CRM)
    public Response<Void> deleteEmployee(@PathVariable("employeeId") Long employeeId) {
        employeeService.deleteEmployee(employeeId);
        return Response.success(null);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询员工列表")
    public Response<PageResult<EmployeeVo>> pageEmployee(@RequestBody PageEmployeeRequest request) {
        return Response.success(employeeService.pageEmployee(request));
    }

    @Operation(summary = "修改员工接单配置")
    @PostMapping("/receiveOrdersConfig/{employeeId}")
    @OperateLog(operateTitle = "修改员工接单配置", operateType = OperateType.UPDATE, businessType = BusinessType.CRM)
    public Response<EmployeeIdVo> receiveOrdersConfig(@PathVariable("employeeId") Long employeeId,
                                                      @RequestBody ReceiveOrdersRequest request) {
        request.setEmployeeId(employeeId);
        return Response.success(employeeService.receiveOrdersConfig(request));
    }

    @PostMapping("/receiveOrdersConfig/page")
    public Response<PageResult<ReceiveOrdersConfigVo>> pageReceiveOrdersConfig(@RequestBody PageEmployeeRequest request) {
        return Response.success(employeeService.pageReceiveOrdersConfig(request));
    }

    @GetMapping("/receiveOrdersConfig/{employeeId}")
    public Response<ReceiveOrdersConfigVo> getReceiveOrdersConfig(@PathVariable("employeeId") Long employeeId) {
        return Response.success(employeeService.getReceiveOrdersConfig(employeeId));
    }
}
